<?php


// set_exception_handler(array('Debug', 'appException'));

class Debug
{
    /**
     * 自定义异常处理
     * @access public
     * @param mixed $e 异常对象
     */
    static public function appException($e)
    {
        $error            = array();
        $error['message'] = $e->getMessage();
        $trace            = $e->getTrace();
        if ('E' == $trace[0]['function']) {
            $error['file'] = $trace[0]['file'];
            $error['line'] = $trace[0]['line'];
        } else {
            $error['file'] = $e->getFile();
            $error['line'] = $e->getLine();
        }
        //处理换行
        $error['trace'] = $e->getTraceAsString();
        //添加实际异常处理逻辑
        sdump($error);
        die; 
    }
}

function sdump($data)
{
    echo "<pre>" . var_export($data, true) . "</pre>";
}

function curlRequest($url, $data = null, $method = 'get', $header = array("content-type: application/json"), $https = false, $timeout = 15)
{
    $method = strtoupper($method);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($https) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
        if (is_string($https) && is_file($https)) {
            curl_setopt($ch, CURLOPT_CAINFO, $https);
        }
    }
    if ($method != "GET") {
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
        }
        if ($method == 'PUT' || strtoupper($method) == 'DELETE') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $result = curl_exec($ch);
    $getinfo = curl_getinfo($ch);
    curl_close($ch);
    return $result;
}
